package cn.simon.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

/**
 * @功能描述： 生产者
 * @Auther: simon-yi'an
 */
@Service
@Slf4j
public class MQsend {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(Object obj){
        log.info("发消息："+obj);
        rabbitTemplate.convertAndSend("queue",obj);
    }

    /**
     * @功能描述: 广播模式
     * @Param:
     * @Return:
     * @Author simon-yi'an
     * @Date 2022/11/6 22:04
     */
    public void sendFanExchange(Object obj){
        log.info("广播："+obj);
        rabbitTemplate.convertAndSend("fanoutExchange","",obj);
    }

    /**
     * @功能描述: 直连模式
     * @Param:
     * @Return:
     * @Author simon-yi'an
     * @Date 2022/11/6 22:03
     */
    public void sendDirectExchangeRed(Object obj){
        log.info("直连(服务键red):"+obj);
        rabbitTemplate.convertAndSend("directExchange","routingkey_red",obj);
    }

    /**
     * @功能描述: 直连模式
     * @Param:
     * @Return:
     * @Author simon-yi'an
     * @Date 2022/11/6 22:05
     */
    public void sendDirectExchangeGreed(Object obj){
        log.info("直连（服务键greed）:"+obj);
        rabbitTemplate.convertAndSend("directExchange","routingkey_greed",obj);
    }

    /**
     * @功能描述: 主题模式 #.simon.# *.simon.# (cn.simon.com)
     * @Param:
     * @Return:
     * @Author simon-yi'an
     * @Date 2022/11/6 22:52
     */
    public void sendTopicExchange01(Object obj){
        log.info("主题（cn.simon.com）:"+obj);
        rabbitTemplate.convertAndSend("topicExchange","cn.simon.com",obj);
    }
    /**
     * @功能描述:主题模式 #.simon.# (simon.com)
     * @Param:
     * @Return:
     * @Author simon-yi'an
     * @Date 2022/11/6 22:58
     */
    public void sendTopicExchange02(Object obj){
        log.info("主题（simon.com）:"+obj);
        rabbitTemplate.convertAndSend("topicExchange","simon.com",obj);

    }
    /**
     * @功能描述: #.simon.# (cn.yian.simon.com)
     * @Param:
     * @Return:
     * @Author simon-yi'an
     * @Date 2022/11/6 23:03
     */
    public void sendTopicExchange03(Object obj){
        log.info("主题（cn.yian.simon.com）:"+obj);
        rabbitTemplate.convertAndSend("topicExchange","cn.yian.simon.com",obj);
    }
    /**
     * @功能描述:头部信息
     * @Param:
     * @Return:
     * @Author simon-yi'an
     * @Date 2022/11/7 0:40
     */
    public void sendHeadersExchange01(String obj){
        log.info("头部信息:" + obj);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color","red");
        properties.setHeader("size","10px");
        Message message = new Message(obj.getBytes(),properties);
        rabbitTemplate.convertAndSend("headerExchange","",message);
    }

    /**
     * @功能描述:头部信息
     * @Param:
     * @Return:
     * @Author simon-yi'an
     * @Date 2022/11/7 0:40
     */
    public void sendHeadersExchange02(String obj){
        log.info("头部信息:" + obj);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color","red");
        properties.setHeader("size","none");
        Message message = new Message(obj.getBytes(),properties);
        rabbitTemplate.convertAndSend("headerExchange","",message);
    }
}
